package com.zhifa.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhifa.Util.ComboboxUtil;
import com.zhifa.Util.ResponseUtil;
import com.zhifa.Util.StringUtil;
import com.zhifa.entity.Institution;
import com.zhifa.service.InstitutionService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/institution")
public class InstitutionController {

	@Resource
	InstitutionService institutionService;
	
	@RequestMapping("/get")
	public String findinstitutionsByParents(@RequestParam("parentid") Integer parentid,HttpServletResponse response) throws Exception{
		List<Institution> Institutions = institutionService.findInstitutionsByParentid(parentid);
		JSONArray comboboxJson=ComboboxUtil.institutioncombobox(Institutions);
		ResponseUtil.write(response, comboboxJson);
		return null;
	}
	
	@RequestMapping("/getall")
	public String findinstitutions(HttpServletResponse response) throws Exception{
		List<Institution> Yinstitutions = institutionService.findInstitutionsByParentid(0);
		JSONArray jsonArray=new JSONArray();
		for (int i = 0; i < Yinstitutions.size(); i++) {
			Institution institution = Yinstitutions.get(i);
			JSONObject jsonObject=new JSONObject();
			jsonObject.put("id", institution.getId());
			jsonObject.put("text",institution.getName());
			jsonArray.add(jsonObject);
		}
		for (int i = 0; i < Yinstitutions.size(); i++) {
			Institution institution = Yinstitutions.get(i);
			List<Institution> Einstitutions = institutionService.findInstitutionsByParentid(institution.getId());
			for (int j = 0; j < Einstitutions.size(); j++) {
				Institution einstitution = Einstitutions.get(j);
				JSONObject jsonObject=new JSONObject();
				jsonObject.put("id", einstitution.getId());
				jsonObject.put("text",einstitution.getName());
				jsonArray.add(jsonObject);
			}
		}
		ResponseUtil.write(response, jsonArray);
		return null;
	}
	//addnewinstitution.action
	@RequestMapping("/addnewinstitution.action")
	public String addnewinstitution(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String institution1 = request.getParameter("institution1");
		Map<String, Object>map=new HashMap<>();
		if (StringUtil.isEmpty(institution1)) {
			map.put("parentid", Integer.parseInt("0"));
		}else {
			map.put("parentid", Integer.parseInt(institution1));
		}
		String institution2 = request.getParameter("institution2");
		map.put("name", institution2);
		int addInstitution = institutionService.addInstitution(map);
		
		JSONObject result=new JSONObject();
		if(addInstitution>0){ 
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
}
